package com.ruoyi.project.module.groupProperty.controller;

import java.util.List;

import com.ruoyi.project.module.group.domain.Group;
import com.ruoyi.project.module.group.service.IGroupService;
import com.ruoyi.project.module.propertyConstraint.domain.PropertyConstraint;
import com.ruoyi.project.module.propertyConstraint.processor.model.PropConstraintVO;
import com.ruoyi.project.module.propertyConstraint.processor.scan.PropConstraintScan;
import com.ruoyi.project.module.propertyConstraint.service.IPropertyConstraintService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.module.groupProperty.domain.GroupProperty;
import com.ruoyi.project.module.groupProperty.service.IGroupPropertyService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

import javax.annotation.Resource;

/**
 * 配置组属性 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-11-10
 */
@Controller
@RequestMapping("/module/groupProperty")
public class GroupPropertyController extends BaseController
{

	final static Logger logger= LoggerFactory.getLogger(GroupPropertyController.class);

    private String prefix = "module/groupProperty";

	@Autowired
	private IPropertyConstraintService propertyConstraintService;

	@Autowired
	private IGroupPropertyService groupPropertyService;

	@Resource
	private IGroupService groupService;

	@Autowired
	PropConstraintScan propConstraintScan;

	@RequiresPermissions("module:groupProperty:view")
	@GetMapping()
	public String groupProperty()
	{
	    return prefix + "/groupProperty";
	}
	
	/**
	 * 查询配置组属性列表
	 */
	@RequiresPermissions("module:groupProperty:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(GroupProperty groupProperty)
	{
		startPage();
        List<GroupProperty> list = groupPropertyService.selectGroupPropertyList(groupProperty);
		return getDataTable(list);
	}

	/**
	 * 查询接口项目列表
	 */
	@RequiresPermissions("module:groupProperty:list")
	@RequestMapping("/listtree/{configGroupId}")
	@ResponseBody
	public List<GroupProperty> listtree(@PathVariable("configGroupId") Long configGroupId,
										GroupProperty groupProperty)
	{
		groupProperty.setConfigGroupId(configGroupId);
		List<GroupProperty> list = groupPropertyService.selectGroupPropertyList(groupProperty);
		return (list);
	}

	/**
	 * 新增配置组属性
	 */
	@GetMapping("/add/{configGroupId}/{parentId}")
	public String add(@PathVariable("configGroupId") Long configGroupId,
					  @PathVariable(value = "parentId",required = false) Long parentId,
					  ModelMap mmap)
	{
		logger.debug("configGroupId="+configGroupId);
		mmap.put("configGroupId", configGroupId);
		int maxOrderNum = groupPropertyService.maxOrderNum(configGroupId, parentId);
		mmap.put("maxOrderNum", maxOrderNum+10);
		Group group = groupService.selectGroupById(configGroupId);
		mmap.put("group", group);

		PropConstraintVO defaultPropConstraint = propConstraintScan.getDefaultPropConstraint();
		if(defaultPropConstraint!=null){
			mmap.put("propertyConstraint",defaultPropConstraint);
		}

		return prefix + "/add";
	}
	
	/**
	 * 新增保存配置组属性
	 */
	@RequiresPermissions("module:group:add")
	@Log(title = "配置组属性", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(GroupProperty groupProperty)
	{
		if(groupProperty.getPushUpdate()==null){
			groupProperty.setPushUpdate(false);
		}
		groupPropertyService.addCompleteRemark(groupProperty);
		return toAjax(groupPropertyService.insertGroupProperty(groupProperty));
	}

	/**
	 * 修改配置组属性
	 */
	@GetMapping("/edit/{configGroupPropertyId}")
	public String edit(@PathVariable("configGroupPropertyId") Long configGroupPropertyId, ModelMap mmap)
	{
		GroupProperty groupProperty = groupPropertyService.selectGroupPropertyById(configGroupPropertyId);
		mmap.put("groupProperty", groupProperty);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存配置组属性
	 */
	@RequiresPermissions("module:group:edit")
	@Log(title = "配置组属性", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	@Transactional
	public AjaxResult editSave(GroupProperty groupProperty)
	{
		if(groupProperty.getPushUpdate()==null){
			groupProperty.setPushUpdate(false);
		}
		int rows = groupPropertyService.updateGroupProperty(groupProperty);
		GroupProperty groupPropertyTemp = groupPropertyService.selectGroupPropertyById(groupProperty.getConfigGroupPropertyId());
		groupPropertyService.addCompleteRemark(groupPropertyTemp);
		return toAjax(rows);
	}
	
	/**
	 * 删除配置组属性
	 */
	@RequiresPermissions("module:group:remove")
	@Log(title = "配置组属性", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	@Transactional
	public AjaxResult remove(Long configGroupId,String ids)
	{		
		return toAjax(groupPropertyService.deleteGroupPropertyByIds(configGroupId,ids));
	}

	@PostMapping("/hasChilds")
	@ResponseBody
	public Boolean hasChilds(Integer uniqueId){
		return groupPropertyService.hasChilds(uniqueId);
	}
}
